package org.jeecg.common.util.pay.wx;


import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.partnerpayments.jsapi.*;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.*;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import org.jeecg.common.util.pay.vo.CcRefundOrderVO;
import org.jeecg.common.util.pay.vo.CcWxPayOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Description:
 * @Author: Planifolia.Van
 * @Date: 2025/1/9 09:11
 */
@Component
public class WxPartnersPayUtil {
    /**
     * 合作伙伴的JSAPI支付
     */
    private final JsapiServiceExtension jsSPApiService;
    /**
     * 退款服务
     */
    private final RefundService refundService;
    /**
     * 支付配置
     */
    @Resource
    private WxPayProperties wxPayProperties;

    /**
     * 构造器注入微信合作伙伴支付
     * @param wxPayConfig 微信支付配置
     */
    @Autowired
    public WxPartnersPayUtil(Config wxPayConfig){
        // 初始化jsAPIServer js支付服务
        this.jsSPApiService = new JsapiServiceExtension.Builder().config(wxPayConfig).build();
        // 初始化退款Server
        this.refundService = new RefundService.Builder().config(wxPayConfig).build();
    }

    /**
     * 创建微信支付订单
     *
     * @return 创建结果
     */
    public PrepayWithRequestPaymentResponse createPayOrder(CcWxPayOrderVO payOrderVO) {
        PrepayRequest prepayRequest = this.orderBaseToRequestConverter(payOrderVO);
        return this.jsSPApiService.prepayWithRequestPayment(prepayRequest,wxPayProperties.getMiniAppId());
    }

    /**
     * 微信进行退款
     *
     * @param refundOrderVO 退款必要的参数
     * @return 退款结果
     */
    public Refund payOrderRefund(CcRefundOrderVO refundOrderVO) {
        CreateRequest createRequest = this.refundOrderVoToRequestConverter(refundOrderVO);
        return this.refundService.create(createRequest);
    }

    /**
     * 将退款的VO类转化成请求
     *
     * @param refundOrderVO 退款传递的参数信息
     * @return 创建出来的结果
     */
    private CreateRequest refundOrderVoToRequestConverter(CcRefundOrderVO refundOrderVO) {
        String orderNo = refundOrderVO.getOrderNo();
        BigDecimal realAmount = refundOrderVO.getPayAmount();
        CreateRequest request = new CreateRequest();
        AmountReq amountReq = new AmountReq();
        amountReq.setRefund(realAmount.longValue());
        amountReq.setTotal(realAmount.longValue());
        amountReq.setCurrency("CNY");
        request.setAmount(amountReq);
        request.setNotifyUrl(wxPayProperties.getNotifyUrl());
        request.setOutRefundNo(orderNo);
        request.setSubMchid(wxPayProperties.getSubMchid());
        return request;
    }



    /**
     * 订单微信支付请求转换器
     *
     * @param payOrderVO 订单基本信息
     * @return 转换后的微信PrepayRequest
     */
    private PrepayRequest orderBaseToRequestConverter(CcWxPayOrderVO payOrderVO) {
        PrepayRequest prepayRequest = new PrepayRequest();
        Amount wxOrderAmount = new Amount();
        Payer payer = new Payer();
        payer.setSpOpenid(payOrderVO.getOpenId());
        int orderAmountIntValue = payOrderVO.getPayAmount().multiply(new BigDecimal(100)).intValue();
        wxOrderAmount.setTotal(orderAmountIntValue);
        prepayRequest.setAmount(wxOrderAmount);
        prepayRequest.setDescription(payOrderVO.getOrderNo());
        prepayRequest.setPayer(payer);
        prepayRequest.setSubMchid(wxPayProperties.getSubMchid());
        prepayRequest.setSubAppid(wxPayProperties.getSubAppid());
        prepayRequest.setSpAppid(wxPayProperties.getMiniAppId());
        prepayRequest.setSpMchid(wxPayProperties.getSpMchid());
        prepayRequest.setOutTradeNo(payOrderVO.getOrderNo());
        prepayRequest.setNotifyUrl(wxPayProperties.getNotifyUrl());
        return prepayRequest;
    }

}
